API Hooking Revealed

[¿ªÀÚÁÖ] ÀÌ ±ÛÀº CodeProject »çÀÌÆ®¿¡ Ivo Ivanov°¡ "API hooking revealed"¶ó´Â Á¦¸ñÀ¸·Î °ÔÀçÇÑ ±ÛÀÔ´Ï´Ù. Win32 ½Ã½ºÅÛ¿¡¼­ API¸¦ ÈÄÅ·ÇÏ´Â ¹æ¹ý¿¡ ´ëÇÑ Àü¹ÝÀûÀÎ ±â¹ýµéÀ» ´Ù·ç°í ÀÖÀ¸¸ç ÀÚ¼¼ÇÑ ¼³¸íÀ̳ª Äڵ庸´Ù´Â °³³äÀûÀÎ ³»¿ëµéÀÌ ¸¹¾Æ Win32 SDK¿¡ Àͼ÷Ä¡ ¾ÊÀº °³¹ßÀڵ鵵 ½±°Ô Á¢±ÙÇÒ ¼ö ÀÖ´Â ÁÁÀº ±ÛÀÎ °Í °°½À´Ï´Ù. ¿ø·¡ ¼Ò½º ÆÄÀÏÀÌ ÇÔ²² ÷ºÎµÇ¾î °ÔÀçµÈ ±ÛÀε¥ ÀÌ ÆÄÀÏÀ» ¹ø¿ªÇÑ ±Û°ú ÇÔ²² ¿Ã¸®´Â °ÍÀº Ȥ½Ã ÀúÀÛ±Ç ¹®Á¦°¡ µÉ ¼öµµ ÀÖ´Ù°í »ý°¢µÇ¾î µû·Î ÷ºÎÇÏÁö ¾Ê¾Ò½À´Ï´Ù. ¼Ò½º ÆÄÀÏÀÌ ÇÊ¿äÇϽŠºÐÀº ¿ø¹®ÀÎ "API hooking revealed"¿¡¼­ ´Ù¿î¹Þ¾Æ »ç¿ëÇϽñ⠹ٶø´Ï´Ù.
ÀÌ ±ÛÀ» Àаí Á¦´ë·Î ÀÌÇØÇÏ·Á¸é ÃÖ¼ÒÇÑ À©µµ¿ìÁî ¸Þ½ÃÁö ÈÄÅ·°ú DLLÀÇ ±âº» ±¸Á¶¿¡ ´ëÇؼ­´Â ¾Ë¾Æ¾ß ÇÕ´Ï´Ù. ÀÌ·± ³»¿ëµéÀº MSDN¿¡ Àß ¼³¸íµÇ¾î ÀÖ°í codeproject, codeguru, devpia µîÀÇ »çÀÌÆ®¿¡µµ ÁÁÀº ±ÛµéÀÌ ¸¹ÀÌ °ÔÀçµÇ¾î ÀÖÀ¸´Ï Âü°íÇϽñ⠹ٶø´Ï´Ù.

¾î¶² À©µµ¿ìÁî ¾îÇø®ÄÉÀ̼ÇÀ» °³¹ßÇϱâ À§Çؼ­ ¿ì¸®µéÀº ¿©·¯ Á¾·ùÀÇ ¾ð¾î³ª µµ±¸µéÀ» »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù. Visual Basic, Visual C++, Delphi, C++ Builder, PowerBuilder µî ¼ö¸¹Àº ¾ð¾î¿Í µµ±¸µéÀÌ ÀÖ½À´Ï´Ù.
ÀÌ·± ÇÁ·Î±×·¥µé¿¡¼­ ¾î¶² ÀÛ¾÷À» ¼öÇàÇϱâ À§Çؼ­ »ç¿ëÇÏ´Â ¹æ¹ýÀº ¼­·Î ´Ù¸¦Áö¶óµµ ±× ³»ºÎ·Î µé¾î°¡ º¸¸é °á±¹¿¡´Â À©µµ¿ìÁî ¿î¿µÃ¼Á¦¿¡¼­ Á¦°øÇÏ´Â API¸¦ È£ÃâÇÏ°Ô µË´Ï´Ù. ¿¹¸¦ µé¾î È­¸é¿¡ ¹®ÀÚ¿­À» Ãâ·ÂÇÏ·Á°í ÇÑ´Ù¸é MFC¿¡¼­´Â CDC::DrawText¸¦ »ç¿ëÇÏ°í Dephi¿¡¼­´Â TCanvas::TextOutÀ» »ç¿ëÇÕ´Ï´Ù. µµ±¸¸¶´Ù »ç¿ëÇÏ´Â ÇüÅ´ ÀÌ·¸°Ô ´Ù¸£Áö¸¸ °á±¹¿¡´Â TextOutA/W API¸¦ È£ÃâÇÕ´Ï´Ù.
TextOutA/W API´Â gdi32.dll¿¡ ±¸ÇöµÇ¾î ÀÖ´Â ÇÔ¼öÀÌ°í ¿î¿µÃ¼Á¦°¡ Á¦°øÇÏ´Â APIÀÔ´Ï´Ù. À©µµ¿ìÁî´Â ±âº»ÀûÀ¸·Î 3°³ÀÇ DLL-kernel32.dll, user32.dll, gdi32.dll-¿¡ ´ëºÎºÐÀÇ API¸¦ ±¸ÇöÇÏ¿© Á¦°øÇÏ°í ¾îÇø®ÄÉÀ̼ÇÀº ½ÇÇà½Ã ÀÚ½ÅÀÇ ÇÁ·Î¼¼½º ÁÖ¼Ò °ø°£À¸·Î À̵é DLLÀ» ¸ÅÇÎÇÑ ÈÄ »ç¿ëÇÕ´Ï´Ù.
API ÈÄÅ·Àº ¾î¶² ÇÁ·Î±×·¥¿¡¼­ API È£ÃâÀ» ÇÏ´Â °ÍÀ» °¡·Îä¼­ °³¹ßÀÚ°¡ ¸¸µç ÇÁ·Î±×·¥ÀÇ ÇÔ¼ö°¡ ó¸®ÇÒ ¼ö ÀÖµµ·Ï ÇÏ´Â ¸ÞÄ«´ÏÁòÀ» ¸»ÇÕ´Ï´Ù. ¹Ýµå½Ã À©µµ¿ìÁîÀÇ APIÀÏ ÇÊ¿ä´Â ¾ø°í ´ÜÁö DLL¿¡¼­ ±¸ÇöÇÏ¿© Á¦°øÇÏ´Â ÇÔ¼öÀÌ¸é µË´Ï´Ù.
API ÈÄÅ·Àº ¾î¶² ¾ð¾î·Î °³¹ßµÈ ÇÁ·Î±×·¥¿¡µµ Àû¿ëµÉ ¼ö ÀÖÀ¸¸ç µð¹ö±ëÀ̳ª Æ®·¹ÀÌ½Ì ÀÛ¾÷, ¸ð´ÏÅ͸µ ÀÛ¾÷ µî¿¡ »ç¿ëÇÒ ¼ö ÀÖÀ¸¸ç ¼Ò½º Äڵ尡 ¾ø´Â ÇÁ·Î±×·¥¿¡ ±â´ÉÀ» Ãß°¡Çϱâ À§ÇÑ ¿ëµµ·Îµµ »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù. ±×¿Ü¿¡µµ ¿©·¯°¡Áö ¿ëµµ·Î »ç¿ëÇÒ ¼ö ÀÖÁö¸¸ ±»ÀÌ ÀÌ·± ÇÁ·Î±×·¥À» °³¹ßÇÒ °èȹÀÌ ¾Æ´Ï´õ¶óµµ À©µµ¿ìÁî ÇÁ·Î±×·¥ÀÇ ±âº»ÀûÀÎ ¿ø¸®¸¦ ÀÌÇØÇϴµ¥ ¸¹Àº µµ¿òÀÌ µË´Ï´Ù.
API ÈÄÅ·À» Àû¿ëÇÏ¿© ¸¸µç ÇÁ·Î±×·¥ Áß¿¡ ÁÖÀ§¿¡¼­ ÀϹÝÀûÀ¸·Î º¼ ¼ö ÀÖ´Â °ÍÀ¸·Î´Â myQuickFind, ¾Æ·¡ÇѱۻçÀü°ú °°Àº ÀüÀÚ »çÀü ÇÁ·Î±×·¥ÀÌ ÀÖ½À´Ï´Ù. ÀÌ·± ÇÁ·Î±×·¥µéÀº ¸ðµÎ ´Ù¸¥ À©µµ¿ìÀÇ ¸¶¿ì½º Ä¿¼­ ¹Ø¿¡ ÀÖ´Â ´Ü¾î¸¦ ÀνÄÇÒ ¼ö ÀÖ´Â ±â´ÉÀ» °¡Áö°í Àִµ¥ ÀÌ°ÍÀº À©µµ¿ìÁî ¸Þ½ÃÁö ÈÄÅ·°ú API ÈÄÅ·(TextOutA/W)À» ÀÌ¿ëÇÑ °ÍÀÔ´Ï´Ù.
¶ÇÇÑ Å©·¡Å·À̳ª ½ºÆÄÀÌ¿þ¾î¸¦ °³¹ßÇϴµ¥ ¾Ç¿ëµÉ ¼öµµ ÀÖ°í ¹Ý´ë·Î ¹æÁöÇÏ´Â ¿ëµµ·Îµµ »ç¿ëµÉ ¼ö ÀÖ½À´Ï´Ù. ¾îÂîµÇ¾úµç °£¿¡ ÀÌ ±ÛÀÌ À©µµ¿ìÁî ȯ°æ¿¡¼­ °³¹ßÀ» ÇϽô ¸¹Àº ºÐµé¿¡°Ô µµ¿òÀÌ µÇ±â¸¦ ¹Ù¶ø´Ï´Ù.

¹ø¿ªÇؼ­ ±ÛÀ» ¿Ã¸®´Â °ÍÀÌ Ã³À½À̾ ¾î»öÇÑ ºÎºÐµéÀÌ ¸¹½À´Ï´Ù. °Ô´Ù°¡ ÀúÀÚ°¡ ½°Ç¥¸¦ »ý·«ÇÏ´Â µ¿°Ý ¸í»ç ±¸¹®À̳ª ºÐ»ç ±¸¹®À» ¸¹ÀÌ »ç¿ëÇÏ¿´°í ¹ø¿ªÀÌ Ç¥ÁØÈ­µÇÁö ¾ÊÀº °³¹ß °ü·Ã ¿ë¾îµéµµ ¸¹¾Æ ´õ¿í ±×·¸½À´Ï´Ù. ±×·¡¼­ ¿ø¹®°ú ¹ø¿ª¹®À» ÇÔ²² ¿Ã·È°í ¹ø¿ªÀÌ ¹ÌºñÇϰųª ¾î»öÇÑ ºÎºÐ¿¡´Â [¿ªÀÚÁÖ]¸¦ ´Þ¾Æ ³õ¾Ò½À´Ï´Ù. ±×·¡µµ ÀÌ»óÇÑ ºÎºÐµéÀº ÄÚ¸àÆ®¸¦ ´Þ¾Æ ÁÖ½Ã¸é °¡´ÉÇÏ¸é ¼öÁ¤Çؼ­ ´Ù½Ã ¿Ã¸®µµ·Ï ÇÏ°Ú½À´Ï´Ù.

Introduction(¼Ò°³)

Intercepting Win32 API calls has always been a challenging subject among most of the Windows developers and I have to admit, it's been one of my favorite topics. The term Hooking represents a fundamental technique of getting control over a particular piece of code execution. It provides an straightforward mechanism that can easily alter the operating system's behavior as well as 3rd party products, without having their source code available.

Win32 API È£ÃâÀ» °¡·Îä´Â °ÍÀº ´ë´Ù¼öÀÇ À©µµ¿ìÁî °³¹ßÀÚµé »çÀÌ¿¡¼­ Ç×»ó µµÀüÇÏ´Â °úÁ¦À̾úÀ¸¸ç ³»°¡ °¡Àå ÁÁ¾ÆÇÏ´Â ÁÖÁ¦ ÁßÀÇ Çϳª°¡ µÇ¾ú´Ù. ÈÄÅ·À̶ó´Â ´Ü¾î´Â ÄÚµå ½ÇÇàÀÇ Æ¯Á¤ ºÎºÐÀ» Á¦¾îÇÒ ¼ö ÀÖ´Â ±â¹Ý ±â¼úÀ» ÀǹÌÇÑ´Ù. ÈÄÅ·Àº ½áµå ÆÄƼ Á¦Ç°»Ó ¾Æ´Ï¶ó ¿î¿µÃ¼Á¦ÀÇ µ¿ÀÛ±îÁöµµ ¼Ò½º ÄÚµå ¾øÀÌ ½±°Ô ¹Ù²Ü ¼ö ÀÖ´Â °£ÆíÇÑ ¸ÞÄ«´ÏÁòÀ» Á¦°øÇÑ´Ù.

Many modern systems draw the attention to their ability to utilize existing Windows applications by employing spying techniques. A key motivation for hooking, is not only to contribute to advanced functionalities, but also to inject user-supplied code for debugging purposes.

¸¹Àº Çö´ëÀûÀÎ ½Ã½ºÅÛµéÀÌ ½ºÆÄÀÌ ±â¼úÀ» »ç¿ëÇÏ¿© ±âÁ¸ÀÇ À©µµ¿ìÁî ¾îÇø®ÄÉÀ̼ÇÀ» È°¿ëÇÏ´Â °Í¿¡ °ü½ÉÀ» ±â¿ïÀÌ°í ÀÖ´Ù. ÈÄÅ·À» ¿¬±¸ÇÏ´Â ÁÖ¿ä µ¿±â´Â Çâ»óµÈ ±â´ÉÀ» Á¦°øÇÏ´Â °Í »Ó ¾Æ´Ï¶ó µð¹ö±ëÀ» ¸ñÀûÀ¸·Î »ç¿ëÀÚ Á¤ÀÇ Äڵ带 ħÅõ½ÃÅ°´Â °Í¿¡ ÀÖ´Ù.

Unlike some relatively "old" operating systems like DOS and Windows 3.xx, the present Windows OS as NT/2K and 9x provide sophisticated mechanisms to separate address spaces of each process. This architecture offers a real memory protection, thus no application is able to corrupt the address space of another process or in the worse case even to crash the operating system itself. This fact makes a lot harder the development of system-aware hooks.

DOS, À©µµ¿ìÁî3.XX¿Í °°ÀÌ »ó´ëÀûÀ¸·Î ¿À·¡µÈ ¿î¿µÃ¼Á¦¿Í ´Þ¸®, NT/2K,9x¿Í °°Àº ÇöÀçÀÇ À©µµ¿ìÁî OS´Â °¢°¢ÀÇ ÇÁ·Î¼¼½ºÀÇ ÁÖ¼Ò °ø°£À» ºÐ¸®ÇÏ´Â Á¤±³ÇÑ ¸ÞÄ«´ÏÁòÀ» Á¦°øÇÑ´Ù. ÀÌ·¯ÇÑ ±¸Á¶´Â ½ÇÁ¦ ¸Þ¸ð¸® º¸È£¸¦ Á¦°øÇÏ¿© ¾î¶°ÇÑ ¾îÇø®ÄÉÀ̼ǵµ ´Ù¸¥ ÇÁ·Î¼¼½ºÀÇ ÁÖ¼Ò °ø°£À» º¯Çü½ÃÅ°°Å³ª ´õ ³ª¾Æ°¡ ¿î¿µ üÁ¦ ÀÚü¸¦ ¸Á°¡¶ß¸®Áö ¸øÇϵµ·Ï ÇÑ´Ù. ÀÌ·¯ÇÑ ÀÌÀ¯·Î ½Ã½ºÅÛ ÈÄÅ·ÀÇ °³¹ßÀº ´õ¿í ¾î·Á¿öÁ³´Ù.

My motivation for writing this article was the need for a really simple hooking framework, that will offer an easy to use interface and ability to capture different APIs. It intends to reveal some of the tricks that can help you to write your own spying system. It suggests a single solution how to build a set for hooking Win32 API functions on NT/2K as well as 98/Me (shortly named in the article 9x) family Windows. For the sake of simplicity I decided not to add a support do UNICODE. However, with some minor modifications of the code you could easily accomplish this task.

ÀÌ ±ÛÀ» ¾²°Ô µÈ µ¿±â´Â »ç¿ëÇϱ⠽±°í ´Ù¸¥ APIµéÀ» °¡·Îç ¼ö ÀÖ´Â ¸Å¿ì °£´ÜÇÑ ÈÄÅ· ÇÁ·¹ÀÓ¿÷¿¡ ´ëÇÑ Çʿ伺 ¶§¹®ÀÌ´Ù. ÀÌ ±Û¿¡¼­ ½ºÆÄÀÌ ½Ã½ºÅÛÀ» ±¸ÃàÇϱâ À§ÇØ ÇÊ¿äÇÑ ¸î°¡Áö Æ®¸¯µéÀ» ¹àÈú °èȹÀÌ´Ù. ÀÌ ±ÛÀº 98/Me¿Í NT/2K ¿¡¼­ ¸ðµÎ »ç¿ëÇÒ ¼ö ÀÖ´Â Win32 API ÇÔ¼ö ÈÄÅ·¿¡ ´ëÇÑ ÇϳªÀÇ ¼Ö·ç¼ÇÀ» Á¦¾ÈÇÑ´Ù. ´Ü¼øÇÏ°Ô Çϱâ À§ÇÏ¿© UNICODE¿¡ ´ëÇÑ Áö¿øÀº Ãß°¡ÇÏÁö ¾Ê±â·Î ÇÏ¿´´Ù. ÇÏÁö¸¸ Äڵ带 ¾à°£ ¼öÁ¤Çϸé UNICODE¿¡ ´ëÇÑ Áö¿øÀ» ½±°Ô ´Þ¼ºÇÒ ¼ö ÀÖÀ» °ÍÀÌ´Ù.

Spying of applications provides many advantages:

¾îÇø®ÄÉÀ̼ÇÀ» ½ºÆÄÀÌÇÏ´Â °ÍÀº ´ÙÀ½°ú °°Àº ¸¹Àº ÀÌÁ¡À» Á¦°øÇÑ´Ù: